package com.youdoneed.user.CoolfaceNotice;
import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.base.common.JWTUtils;
import com.youdoneed.user.model.CoolfaceNotice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by lid on 2017/11/14 0014.
 * @author lid
 * @since 2017-11-14
 **/
@RestController
@RequestMapping("/coolfaceNotice")
@Api(description = "coolface消息控制器")
public class CoolfaceNoticeController {

    private static final Logger logger = LoggerFactory.getLogger(CoolfaceNoticeController.class);

    @Autowired
    private CoolfaceNoticeService coolfaceNoticeService;

    /**
     * 保存coolface通知
     * @return ResultMessage
     * @author lid
     * @date 2017.11.14
     */
    @ResponseBody
    @RequestMapping(value = "/saveNotice" , method = {RequestMethod.POST})
    @ApiOperation(notes = "保存通知" , value = "保存通知")
    public ResultMessage<?> saveNotice(@RequestBody CoolfaceNotice coolfaceNotice){
        coolfaceNoticeService.saveCoolfaceNotice(coolfaceNotice);
        return new ResultMessage<>("保存通知成功");
    }

    /**
     * 获取我的coolface通知
     * @return ResultMessage
     * @author lid
     * @date 2017.11.14
     */
    @ResponseBody
    @RequestMapping(value = "/getNotice" , method = {RequestMethod.POST})
    @ApiOperation(notes = "获取我的coolface通知" , value = "获取我的coolface通知")
    public ResultMessage<List<CoolfaceNotice>> getNotice(@RequestHeader(value = "Authorization") String token){
        String userId = JWTUtils.getTokenUserId(token);
        if(StringUtils.hasText(userId)){
            logger.info("userId:"+userId);
        }
        List<CoolfaceNotice> list = coolfaceNoticeService.getCoolfaceNoticeByUserId(userId);
        return new ResultMessage<>(list);
    }

    @ResponseBody
    @RequestMapping(value = "/readCoolfaceNotice" , method = {RequestMethod.POST})
    @ApiOperation(notes = "将消息设置为已读" , value = "将消息设置为已读")
    public ResultMessage<?> readCoolfaceNotice(@RequestParam("noticeId") String noticeId){
        coolfaceNoticeService.readCoolfaceNotice(noticeId);
        return new ResultMessage<>("将消息设置为已读成功");
    }
}
